<?php

namespace app\command;

use app\helper\Schema;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class SchemaDiffCommand extends Command {

    use \app\helper\SqlTraits;

    public function configure() {
        $this->setName('schema:diff')->setDescription('对比两个数据的结构,生成差异的sql文件')
                ->addArgument('old', InputArgument::OPTIONAL, '老数据库的名称', DBNAME)
                ->addArgument('new', InputArgument::OPTIONAL, '新数据库的名称', 'cmstop_schema');
    }

    public function execute(InputInterface $input, OutputInterface $output) {
        $newSchema = $input->getArgument('new');
        $oldSchema = $input->getArgument('old');
        $start_time = time();
        try {
            Schema::getInstance($newSchema, $oldSchema)->make();
        } catch (\Exception $exc) {
            $output->writeln(sprintf('<error>文件对比失败:%s</error>', $exc->getMessage()));
            exit;
        }
        if (static::parseFile(DIFFSQL)) {
            $output->writeln(sprintf('<info>数据库结构对比完成(%s):耗时%ds</info>', realpath(DIFFSQL),time() - $start_time));
        } else {
            $output->writeln('<info>数据库结构对比完成:无任何差异</info>');
        }
    }

}
